<?php
	ob_start();
	include 'validationfunctions.php';
	include 'savegoalfunction.php';
	include 'head.html';
?>
<body>
<div id="wrapper">
	<header>
		<a href="index.php?logout=true">Log out</a>
	</header>
	<div id="content">
		<h1>Add new goal</h1>
<?php
	$goalname="";
	$measurename="%";
	$measuretype="";
	$startvalue="0";
	$currentvalue="-";
	$targetvalue="100";
	$priority="";
	$deadline="";
	$description="";
	
	$output_form = false;
	#session_start();
	if (isset($_POST['submit'])){
		#form has been submitted	
		
			$goalname = $_POST['goal'];
			$measurename = $_POST['measurename'];			
			if (isset($_POST['measuretype'])){
				$measuretype = $_POST['measuretype'];
			}else{
				$measuretype = "5";
			}			
			$startvalue = $_POST['startvalue'];
			$currentvalue = $_POST['currentvalue'];
			$targetvalue = $_POST['targetvalue'];
			$priority = $_POST['priority'];
			$deadline = $_POST['deadline'];
			$description = $_POST['description'];
			$userid = "";
			if(isset($_GET['userid'])){
					$userid = $_GET['userid'];
			}
			
			// validation calls
			
			$errorListRequieredFields = array();
			$errorGoalNameExists = "";
			$errorEqualsSubgoalNamesExist = "";
			$errorListDeadlines = array();
			$errorListGoalMeasureValues = array();
			$errorListSubgoalValues = array();
			
			$errorListRequieredFields = validateRequiredFields(false);
			$errorGoalNameExists = validateExistingGoalName($goalname, $userid);
			$errorEqualsSubgoalNamesExist = validateEqualSubgoalNames();
			$errorListDeadlines = validateDeadlines();
			
			$startValue = $_POST['startvalue'];
			$currentValue = $_POST['currentvalue'];
			$targetValue = $_POST['targetvalue'];
			$errorListGoalMeasureValues = validateGoalValues($measuretype, $startValue, $currentValue, $targetValue);
			
			$errorListSubgoalValues = validateSubgoalsValues();
			
			if (count($errorListRequieredFields) == 0 &&
			 			$errorGoalNameExists == "" &&
			  			$errorEqualsSubgoalNamesExist == "" &&
						count($errorListDeadlines) == 0 &&
						count($errorListGoalMeasureValues) == 0 &&
						count($errorListSubgoalValues) == 0){
				
				
				saveGoalIntoDatabase($goalname,$description,$startvalue,$currentvalue,$targetvalue,$priority,$measurename,$measuretype, $deadline, $userid);
				$output_form = false;
				header("Location:index.php?page=overview&back=1");
				ob_end_flush();
			}else{
				echo "<div class=\"errors\">";
				echo "<p>";
				foreach ($errorListRequieredFields as $i => $value) { 
					echo $errorListRequieredFields[$i].'<br/>';
				}
				
				if($errorGoalNameExists != "" || $errorEqualsSubgoalNamesExist != ""){
					echo $errorGoalNameExists.'<br/>';
					echo $errorEqualsSubgoalNamesExist.'<br/>';
				}				
				foreach ($errorListDeadlines as $i => $value) {
					echo $errorListDeadlines[$i].'<br/>';
				}
				
				foreach ($errorListGoalMeasureValues as $i => $value) {
					echo $errorListGoalMeasureValues[$i].'<br/>';
				}
				foreach ($errorListSubgoalValues as $i => $value) {
					echo $errorListSubgoalValues[$i].'<br/>';
				}
				echo "</p>";
				echo "</div>";
			}
			
			$output_form = true;
	}
	
	if ($output_form == true){
?>
	<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" >
		<label>Goal *</label>
		<input name="goal" type="text" value="<?php echo $goalname; ?>"/>		
		<br />
		<label>Description </label>
		<textarea name="description" type="text"><?php echo $description; ?></textarea>	
		<br />
		<label>Deadline (YYYY-MM-DD) </label>
		<input name="deadline" type="date" value="<?php echo $deadline; ?>"/>
		<br />
		<label>Priority *</label>
		<select name="priority">
			<option name="urgimp" value="ui" <?php if ($priority == "ui"){echo "selected";}?>>1 - urgent / important</option>			
			<option name="urgnimp" value="uni" <?php if ($priority == "uni"){echo "selected";}?>>2 - urgent / unimportant</option>
			<option name="nurgimp" value="nui" <?php if ($priority == "nui"){echo "selected";}?>>3 - unurgent / important</option>
			<option name="nurgnimp" value="nuni" <?php if ($priority == "nuni"){echo "selected";}?>>4 - unurgent / unimportant</option>
		</select>
		<br />
		<label>Measure name *</label>		
		<input id="measurename" name="measurename" type="text" value="<?php echo $measurename; ?>" 
		<?php if (isset($_POST['subgoalname_0'])){echo "readonly=true";}?> />
		<br />		
		<label>Measure type *</label>		 
		<select id="measuretype" name="measuretype" <?php if (isset($_POST['subgoalname_0'])){echo "readonly=\"readonly\"";}?> onchange="goalTypeChosen(this);">
			<option name="percent" value="5" <?php if ($measuretype == "5"){echo "selected";}?>>percent</option>
			<option name="integer" value="1" <?php if ($measuretype == "1"){echo "selected";}?>>integer</option>
			<option name="float" value="2" <?php if ($measuretype == "2"){echo "selected";}?>>float</option>
			<option name="money" value="3" <?php if ($measuretype == "3"){echo "selected";}?>>money</option>
			<option name="yesno" value="4" <?php if ($measuretype == "4"){echo "selected";}?>>yes/no</option>			
		</select>
		<br />
		<label>Start value *</label>
		<input id="startvalue" name="startvalue" type="text" value="<?php echo $startvalue; ?>" <?php if (isset($_POST['subgoalname_0'])){echo "readonly=true";}?> />
		<br />
		<label>Current value *</label>
		<input id="currentvalue" name="currentvalue" type="text" value="<?php echo $currentvalue; ?>" <?php if (isset($_POST['subgoalname_0'])){echo "readonly=true";}?> />
		<br />
		<label>Target value *</label>
		<input id="targetvalue" name="targetvalue" type="text" value="<?php echo $targetvalue; ?>" <?php if (isset($_POST['subgoalname_0'])){echo "readonly=true";}?> />
		<br />		
		<hr/>
		<table class="colored">
			<thead>
    			<tr>
					<th>Subgoal<br/>Name*</th>
					<th>Measure<br/>Name*</th>
					<th>Measure<br/>Type*</th>
					<th>Start<br/>Value*</th>
					<th>Current<br/>Value*</th>
					<th>Target<br/>Value*</th>
					<th>Priority*</th>
					<th>Deadline<br/>(YYYY-MM-DD)</th>
				</tr>
			</thead>
			<tbody id="subgoalstbody">
		
		<!--lines for subgoals-->
		<?php
			if (isset($_POST['subgoalname_0'])){
				$numberOfsubgoals = intval((sizeof($_POST) - 9 )/ 8);
				for ($i = 0 ; $i < $numberOfsubgoals ; $i++){
					$tempColumnName = "subgoalname_"."$i";			
					$subgoalname = $_POST[$tempColumnName];
					$tempColumnName = "subgoalstartvalue_"."$i";
					$subgoalstartvalue = $_POST[$tempColumnName];
					$tempColumnName = "subgoalcurrentvalue_"."$i";
					$subgoalcurrentvalue = $_POST[$tempColumnName];
					$tempColumnName = "subgoaltargetvalue_"."$i" ;
					$subgoaltargetvalue = $_POST[$tempColumnName];
					$tempColumnName = "subgoalpriority_"."$i";
					$subgoalpriority = $_POST[$tempColumnName];
					$tempColumnName = "subgoalmeasurename_"."$i";
					$subgoalmeasurename = $_POST[$tempColumnName];
					$tempColumnName = "subgoalmeasuretype_"."$i";
					$subgoalmeasuretype = $_POST[$tempColumnName];
					$subgoaldeadline = "";
					
					
					if (isset($_POST["subgoaldeadline_"."$i"])){
						$subgoaldeadline = $_POST["subgoaldeadline_"."$i"];
					}
		?>

				<tr id="<?php echo "trsubgoal_"."$i";?>">
					<td><input name="<?php echo "subgoalname_"."$i";?>" type="text" value="<?php echo "$subgoalname"; ?>"/></td>
					<td><input id="subgoalmeasurename_<?php echo $i;?>" name="<?php echo "subgoalmeasurename_"."$i";?>" type="text" value=" <?php echo "$subgoalmeasurename"; ?>"/></td>
					<td>
						<select id="select_<?php echo $i;?>" name="<?php echo "subgoalmeasuretype_"."$i";?>" onchange="subgoalTypeChosen(this);">
							<option name="integer" value="1" <?php if($subgoalmeasuretype == "1"){echo "selected";}?>>integer</option>
							<option name="float" value="2" <?php if($subgoalmeasuretype == "2"){echo "selected";}?>>float</option>
							<option name="money" value="3" <?php if($subgoalmeasuretype == "3"){echo "selected";}?>>money</option>
							<option name="yesno" value="4" <?php if($subgoalmeasuretype == "4"){echo "selected";}?>>yes/no</option>
							<option name="percent" value="5" <?php if($subgoalmeasuretype == "5"){echo "selected";}?>>percent</option>
						</select>
					</td>
					<td><input id="subgoalstart_<?php echo $i;?>" name="<?php echo "subgoalstartvalue_"."$i";?>" type="text" value="<?php echo "$subgoalstartvalue"; ?>"/></td>
					<td><input id="subgoalcurrent_<?php echo $i;?>" name="<?php echo "subgoalcurrentvalue_"."$i";?>" type="text" value="<?php echo "$subgoalcurrentvalue"; ?>"/></td>
					<td><input id="subgoaltarget_<?php echo $i;?>" name="<?php echo "subgoaltargetvalue_"."$i";?>" type="text" value="<?php echo "$subgoaltargetvalue"; ?>"/></td>
					<td>
						<select name="<?php echo "subgoalpriority_"."$i";?>">
							<option name="urgimp" value="ui" <?php if($subgoalpriority == "ui"){echo " selected";}?>>1 - urgent / important</option>
							<option name="urgnimp" value="uni" <?php if($subgoalpriority == "uni"){echo " selected";}?>>2 - urgent / unimportant</option>
							<option name="nurgimp" value="nui" <?php if($subgoalpriority == "nui"){echo " selected";}?>>3 - unurgent / important</option>
							<option name="nurgnimp" value="nuni" <?php if($subgoalpriority == "nuni"){echo " selected";}?>>4 - unurgent / unimportant</option>
						</select>
					</td>
					<td><input name="<?php echo "subgoaldeadline_"."$i";?>" type="date" value="<?php echo "$subgoaldeadline"; ?>"/></td>
				</tr>
			
		
		<?php }} // end of "for-loop" and "if (isset($subgoalname_0))"?>
		</tbody>
		</table>
		<br />
		<hr/>
		<br />
		<input type="button" value="Add new subgoal" onclick="addsubgoal()"/>
		<input type="button" value="Delete last subgoal" onclick="deletesubgoal()"/>
		<input name="submit" type="submit" value="Create goal"/>	
	</form>	
<?php }else{
?>	
		
		<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" >
		<label>Goal *</label>
		<input name="goal" type="text" value=""/>		
		<br />
		<label>Description </label>
		<textarea name="description" type="text"></textarea>
		<br />
		<label>Deadline (YYYY-MM-DD) </label>
		<input name="deadline" type="date" value=""/>
		<br />
		<label>Priority *</label>
		<select name="priority">
			<option name="urgimp" value="ui" selected>1 - urgent / important</option>
			<option name="urgnimp" value="uni">2 - urgent / unimportant</option>
			<option name="nurgimp" value="nui">3 - unurgent / important</option>
			<option name="nurgnimp" value="nuni">4 - unurgent / unimportant</option>
		</select>
		<br />
		<label>Measure name *</label>
		<input id="measurename" name="measurename" type="text" value="%" readonly=true />
		<br />
		<label>Measure type *</label>
		<select id="measuretype" name="measuretype" disabled onchange="goalTypeChosen(this);">
			<option name="percent" value="5" selected>percent</option>
			<option name="integer" value="1">integer</option>
			<option name="float" value="2">float</option>
			<option name="money" value="3">money</option>
			<option name="yesno" value="4">yes/no</option>			
		</select>
		<br />
		<label>Start value *</label>
		<input id="startvalue" name="startvalue" type="text" value="0" readonly=true />
		<br />
		<label>Current value *</label>
		<input id="currentvalue" name="currentvalue" type="text" value="0" readonly=true />
		<br />
		<label>Target value *</label>
		<input id="targetvalue" name="targetvalue" type="text" value="100" readonly=true />
		<br />		
		
		
		<!--table für subgoals-->
		<hr/>
		<table class="colored">
			<thead>
    			<tr>
					<th>Subgoal<br/>Name*</th>
					<th>Measure<br/>Name*</th>
					<th>Measure<br/>Type*</th>
					<th>Start<br/>Value*</th>
					<th>Current<br/>Value*</th>
					<th>Target<br/>Value*</th>
					<th>Priority*</th>
					<th>Deadline<br/>(YYYY-MM-DD)</th>
				</tr>
			</thead>
			<tbody id="subgoalstbody">
				<tr id="trsubgoal_0">
					<td><input name="subgoalname_0" type="text" value=""/></td>
					<td><input id="subgoalmeasurename_0" name="subgoalmeasurename_0" type="text" value=""/></td>
					<td>
						<select id="select_0" name="subgoalmeasuretype_0" onchange="subgoalTypeChosen(this);">
							<option name="integer" value="1">integer</option>
							<option name="float" value="2">float</option>
							<option name="money" value="3">money</option>
							<option name="yesno" value="4">yes/no</option>
							<option name="percent" value="5">percent</option>
						</select>
					</td>
					<td><input id="subgoalstart_0" name="subgoalstartvalue_0" type="text" value=""/></td>
					<td><input id="subgoalcurrent_0" name="subgoalcurrentvalue_0" type="text" value=""/></td>
					<td><input id="subgoaltarget_0" name="subgoaltargetvalue_0" type="text" value=""/></td>
					<td>
						<select name="subgoalpriority_0">
							<option name="urgimp" value="ui">1 - urgent / important</option>
							<option name="urgnimp" value="uni">2 - urgent / unimportant</option>
							<option name="nurgimp" value="nui">3 - unurgent / important</option>
							<option name="nurgnimp" value="nuni">4 - unurgent / unimportant</option>
						</select>
					</td>
					
					<td><input name="subgoaldeadline_0" type="date" value=""/></td>
				</tr>
			</tbody>
		</table>
		<br />
		<hr/>
		<br />
		<input type="button" value="Add new subgoal" onclick="addsubgoal()"/>
		<input type="button" value="Delete last subgoal" onclick="deletesubgoal()"/>
		<input name="submit" type="submit" value="Create goal"/>	
	</form>	
	
	
<?php } ?>	
				<a href="index.php?page=overview&back=1">goals overview</a>
			</div>
		<footer></footer>
	</div>
</body>





